package com.xhd.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;

import com.xhd.exception.XFileException;
import com.xhd.server.ServerConfig;
import com.xhd.util.XDate;

public class XFileUtil {
	public static void copyFile(String paramString1, String paramString2)
			throws XFileException {
		File localFile1 = new File(paramString1);
		if (!(localFile1.exists()))
			throw new XFileException(60002, localFile1.getName());
		File localFile2 = new File(paramString2);
		File localFile3 = localFile2.getParentFile();
		if ((!(localFile3.exists())) || (!(localFile3.isDirectory())))
			throw new XFileException(60013);
		FileInputStream localFileInputStream = null;
		FileOutputStream localFileOutputStream = null;
		try {
			localFileInputStream = new FileInputStream(localFile1);
			int i = 32768;
			byte[] arrayOfByte = new byte[i];
			localFileOutputStream = new FileOutputStream(localFile2);
			while (true) {
				i = localFileInputStream.read(arrayOfByte);
				if (i <= 0)
					break;
				localFileOutputStream.write(arrayOfByte, 0, i);
			}
			localFileOutputStream.flush();
			localFileInputStream.close();
			localFileOutputStream.close();
		} catch (IOException localIOException) {
			throw new XFileException(60027, localFile1.getPath());
		}
	}

	public static void createDir(String paramString1, String paramString2)
			throws XFileException {
		File localFile = new File(paramString1);
		if ((!(localFile.exists())) || (!(localFile.isDirectory())))
			throw new XFileException(60022);
		String str = localFile.getPath();
		if (ServerConfig.FILE_SEPARATOR != '/')
			paramString2 = paramString2.replace('/',
					ServerConfig.FILE_SEPARATOR);
		str = str + ServerConfig.FILE_SEPARATOR + paramString2;
		localFile = new File(str);
		if (localFile.exists()) {
			if (localFile.isDirectory())
				return;
			throw new XFileException(60023, str);
		} else {
			try {
				if (localFile.mkdirs())
					return;
				throw new XFileException(60023, str);
			} catch (Exception localException) {
				throw new XFileException(60023, str);
			}
		}
	}

	public static void deleteDir(String paramString, boolean paramBoolean)
			throws XFileException {
		File localFile = new File(paramString);
		if ((!(localFile.exists())) || (!(localFile.isDirectory())))
			throw new XFileException(60022);
		File[] arrayOfFile = localFile.listFiles();
		if (arrayOfFile != null)
			for (int i = 0; i < arrayOfFile.length; ++i) {
				if (arrayOfFile[i].isDirectory())
					deleteDir(arrayOfFile[i].getPath(), false);
				arrayOfFile[i].delete();
			}
		if (!(paramBoolean))
			return;
		localFile.delete();
	}

	public static String getModifiedTime(String paramString) {
		String str = null;
		File localFile = new File(paramString);
		if (localFile.exists()) {
			XDate localDate = new XDate(localFile.lastModified());
			str = localDate.toText();
		}
		return str;
	}

	public static Vector listFiles(String paramString1, String paramString2) {
		File localFile = new File(paramString1);
		if ((localFile.exists()) && (localFile.isDirectory())) {
			Vector localVector = new Vector();
			File[] arrayOfFile = localFile.listFiles();
			int i = 0;
			if ((arrayOfFile != null) && (arrayOfFile.length > 0)) {
				if (paramString2 != null)
					paramString2 = paramString2.toLowerCase();
				i = 0;
			}
			while (true) {
				if (arrayOfFile[i].isFile()) {
					String str = arrayOfFile[i].getName();
					if ((paramString2 == null)
							|| (str.toLowerCase().endsWith(paramString2))) {
						Hashtable localHashtable = new Hashtable();
						localHashtable.put("filename", str);
						localHashtable.put("filesize",
								String.valueOf(arrayOfFile[i].length()));
						XDate localDate = new XDate(
								arrayOfFile[i].lastModified());
						localHashtable.put("updatetime", localDate.toText());
						localVector.add(localHashtable);
					}
				}
				++i;
				if (i >= arrayOfFile.length)
					return localVector;
			}
		}
		return null;
	}

	public static void moveFile(String paramString1, String paramString2)
			throws XFileException {
		File localFile1 = new File(paramString1);
		if (!(localFile1.exists()))
			throw new XFileException(60002, localFile1.getName());
		File localFile2 = new File(paramString2);
		File localFile3 = localFile2.getParentFile();
		if ((!(localFile3.exists())) || (!(localFile3.isDirectory())))
			throw new XFileException(60013);
		FileInputStream localFileInputStream = null;
		FileOutputStream localFileOutputStream = null;
		try {
			localFileInputStream = new FileInputStream(localFile1);
			int i = 32768;
			byte[] arrayOfByte = new byte[i];
			localFileOutputStream = new FileOutputStream(localFile2);
			while (true) {
				i = localFileInputStream.read(arrayOfByte);
				if (i <= 0)
					break;
				localFileOutputStream.write(arrayOfByte, 0, i);
			}
			localFileOutputStream.flush();
			localFileInputStream.close();
			localFileOutputStream.close();
		} catch (IOException localIOException) {
			throw new XFileException(60027, localFile1.getPath());
		}
		localFile1.delete();
	}
}
